<?php

class YumActivityLog extends EMongoDocument
{
	/**
	 * A public variable should be defined for each key=>value you want in the
	 * model. Just like if it were a column in a mysql database
	 */
	public $id;
	public $user_id;
	public $action;
	public $timestamp;
	public $message;
	public $ip_address;
	public $params;
	public $username; 
	public $count;
	public $date;
	public $from;
	
	const LOG_USER_PLAY_GAME 					= "UserPlayGame";
	const LOG_GENERATE_GAME_LINK  				= "GenerateLinkGame";
	const LOG_LOGIN_CMS		 					= "LoginCms";
	const LOG_REFERER 							= "RefererUrl";
	
	/**
	 * this is similar to the get tableName() method. this returns tha name of the
	 * document for this class. this should be in all lowercase.
	 */
	public function getCollectionName()
	{
		return 'activities';
	}
	/**
	 * @see EMongoDocument::getMongoDBComponent()
	 */
	public function getMongoDBComponent()
	{
		return Yii::app()->getComponent('mongodb_game');
	}
	/**
	 * If we override this method to return something different than '_id',
	 * internal methods as findByPk etc. will be using returned field name as a primary key
	 * @return string|array field name of primary key, or array for composited key
	 */
	public function primaryKey()
	{
		return 'id';
	}

	/**
	 * This is defined as normal. Nothing has changed here
	 *
	 * @return array
	 */
	public function rules() {
		return array(
			array('user_id, action, from , timestamp, ip_address', 'required'),
		);
	}

	/**
	 * This returns attribute labels for each public variable that will be stored
	 * as key in the database. Is defined just as normal with mysql
	 *
	 * @return array
	 */
	public function attributeLabels()
	{
		return array(
			'user_id'		=> 'User',
			'action'		=> 'Action',
			'timestamp'		=> 'Time',
			'message'		=> 'Message',
			'ip_address'	=> 'IP Adress',
			'params'		=> 'Params'
		);
	}

	/**
	 * Returns the class name just as nornal.
	 *
	 * @static
	 * @param string $className
	 * @return
	 */
	public static function model($className = __CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * BELONGS TO relation
	 * @return
	 */
	public function user()
	{
		return YumUser::model()->findByPk($this->user_id);
	}
	
	/**
	 * Embedded Documents. All you need to do is list the models that will be used
	 * as embedded documents in this fashion:
	 * 'name' => 'Model'
	 * Where 'name' is the name of this embedded document, and how it will appear
	 * as a key in the document.And 'Model' is the name of the model that holds
	 * the embedded documents information. For the example below, see the
	 * UserAddress.php file for an example
	 *
	 * @return array
	 */
	public function embeddedDocuments()
	{
		// property name => embedded document class name
		return array(
			
		);
	}
	
	public function searchLog() {
		$criteria = new EMongoCriteria();
		
		if (! empty($this->username)) {
			$criteria->params = new MongoRegex('/' . $this->username . '/i');
		}
		
		if (! empty($this->action)) 
			$criteria->action = new MongoRegex('/' . $this->action . '/i');
			
		if (! empty($this->ip_address)) 
			$criteria->ip_address = new MongoRegex('/' . $this->ip_address . '/i');
			
		if (! empty($this->timestamp)) {
			$criteria->addCond('timestamp','>=', strtotime(date(UserModule::$dateTimeFormat, strtotime($this->timestamp.' 00:00:00'))));
			$criteria->addCond('timestamp','<=', strtotime(date(UserModule::$dateTimeFormat, strtotime($this->timestamp.' 23:59:59'))));
		}
		
		$criteria->sort('timestamp', EMongoCriteria::SORT_DESC);
		
		$dataProvider = new EMongoDocumentDataProvider('YumActivityLog', array(
			'criteria' => $criteria,
			'pagination'=>array(
		        'pageSize'=>50,
		    ),
		));
		
		return $dataProvider;
	}
	
	public function getMessage() {
		return ($this->params) ? Yum::t($this->message, (array)json_decode($this->params)) : Yum::t($this->message);
	}
}
